package com.rom.ratethehell;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.rom.ratethehell.api.services.sneakapic.model.ResponsesUpdatePicturesResponse;
import com.rom.ratethehell.entities.ServerResponse;
import com.rom.ratethehell.protocol.ImageAdapter;
import com.rom.ratethehell.protocol.Profile;
import com.rom.ratethehell.protocol.Thumbnail;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.TabSpec;

import com.rom.ratethehell.protocol.RequestOrganizer;
import com.rom.ratethehell.protocol.tasks.GetProfileTask;
import com.rom.ratethehell.protocol.tasks.SendImageTask;
import com.rom.ratethehell.protocol.tasks.SneakAPicTask;

public class ProfileActivity extends Activity {

	private static final int REQUEST_CODE = 0;
	protected static final String KEY_PROFILE_TO_SHOW = "KPTS";
	private GridView gridview;
	private String profileOf;
	private Profile showingProfile;
	private SharedPreferences mSharedPrefs;
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
    public void pickImage() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, REQUEST_CODE);
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_profile);
		Button sendPicButton=(Button) findViewById(R.id.send_pic_button);
		Button sendMssgButton=(Button) findViewById(R.id.send_mssg);
		Button logoutButton=(Button) findViewById(R.id.logout);
		profileOf = getIntent().getStringExtra(KEY_PROFILE_TO_SHOW);
		final EditText mssgBody = (EditText)findViewById(R.id.mssg_body);
		if (profileOf.equals(Global.userName)) {
			sendMssgButton.setVisibility(View.INVISIBLE);
			mssgBody.setVisibility(View.INVISIBLE);
			
		} else {
			sendPicButton.setVisibility(View.INVISIBLE);
		}
		sendMssgButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//RequestOrganizer.getInstance().sendMessege(mssgBody.getText().toString(), profileOf, Global.userName);
			}
		});
	logoutButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				(new SneakAPicTask<ServerResponse>(ProfileActivity.this) {

					@Override
					protected void doTask() throws Exception {
						setResult(RequestOrganizer.getInstance().logoff());
					}

					@Override
					protected void onSuccess(ServerResponse result) {
						mSharedPrefs=getSharedPreferences(Global.SHARED_PREFS_KEY, MODE_PRIVATE);
						mSharedPrefs.edit().putBoolean(Global.SHARED_PREFS_KEY_LOGGED_IN, false).commit();
						Intent intent = new Intent(ProfileActivity.this,LoginActivity.class);
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
						startActivity(intent);
					};
				}).execute();
	        	
			}
		});
	    gridview = (GridView) findViewById(R.id.gridview);
	    (new GetProfileTask(this,profileOf) {
			
			

			@Override
			protected void onSuccess(Profile result) {
				gridview.setAdapter(new ImageAdapter(ProfileActivity.this,showingProfile = result));
			}
			
		}).execute();
	    

	    gridview.setOnItemClickListener( new OnItemClickListener(){
	        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	        	Intent intent = new Intent(ProfileActivity.this,RateActivity.class);
	        	intent.putExtra(RateActivity.RATE_INTENT_PIC_ID_KEY, id);
				startActivity(intent);
	        }
	    });
	    
		sendPicButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				pickImage();
			}
		});
    	TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
    	tabHost.setup();

    	TabSpec spec1=tabHost.newTabSpec("Main");
    	spec1.setContent(R.id.feed);
    	spec1.setIndicator("Main");

    	TabSpec spec2=tabHost.newTabSpec("Photos");
    	spec2.setIndicator("Photos");
    	spec2.setContent(R.id.photos);

    	TabSpec spec3=tabHost.newTabSpec("Info");
    	spec3.setIndicator("Info");
    	spec3.setContent(R.id.info);

    	tabHost.addTab(spec1);
    	tabHost.addTab(spec2);
    	tabHost.addTab(spec3);
    	}
    @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
			(new SendImageTask(this,data.getData()) {



				@Override
				protected void onSuccess(ArrayList<Thumbnail> result) {
					showingProfile.addThumbnails(result);
				}

			}).execute();
			
		}
        super.onActivityResult(requestCode, resultCode, data);
    }
}
